COVID-19
Multi-Layer Networks and Backpropagation
from datetime import datetime
currentSecond= datetime.now().second
currentMinute = datetime.now().minute
currentHour = datetime.now().hour
currentDay = datetime.now().day
currentMonth = datetime.now().month
currentYear = datetime.now().year
import pandas as pd
import xlrd
url = "https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-"+str(currentYear)+"-0"+str(currentMonth)+"-"+str(currentDay)+".xlsx"
#df = pd.read_csv(url)
data = pd.read_excel(url)
data.head()
data['dateRep'].max()
data.dtypes
import numpy as np
data['dateRep']= pd.to_datetime(data['dateRep'])
#data['dateRep']= pd.Series(data['dateRep'])
data_min = data[data.cases > 0].reset_index(drop=True)
data_min.head()
min_date = data_min.groupby(['countriesAndTerritories'], as_index=False)["dateRep"].min()
min_date = min_date.rename(columns={'dateRep': 'min_date'})
min_date.head()
df = data.merge(min_date, on='countriesAndTerritories', how='left')
df.head()
#df['DateRep']= pd.to_datetime(df['DateRep'])
df['min_date']= pd.to_datetime(df['min_date'])
df.head()
df['days'] = (df['dateRep'] - df['min_date']) / np.timedelta64(1, 'D')
df.head()
df.sort_values(by=['countriesAndTerritories', 'dateRep'], inplace=True)
df.head()
#df = pd.read_csv(StringIO(data))
cumsums = df.groupby(['countriesAndTerritories', 'dateRep'])["cases"].sum().fillna(0).groupby(level=0).cumsum()
df.set_index(['countriesAndTerritories', 'dateRep'], inplace=True)
df['cum_cases'] = cumsums
df.reset_index(inplace=True)
df.head(100)
cumsums = df.groupby(['countriesAndTerritories', 'dateRep'])["deaths"].sum().fillna(0).groupby(level=0).cumsum()
df.set_index(['countriesAndTerritories', 'dateRep'], inplace=True)
df['cum_deaths'] = cumsums
df.reset_index(inplace=True)
df.head(100)
df['date_int'] = (df['year'] * 10000 + df['month'] * 100 + df['day']).astype(int)
df = df.rename(columns={'countriesAndTerritories': 'pais'})
df = df.sort_values(by=['date_int', 'pais'])
max_cases = df.groupby(['pais'], as_index=False)["cum_cases"].max()
max_cases = max_cases.rename(columns={'cum_cases': 'max_cases'})
df = df.merge(max_cases, on='pais', how='left')
df.head()
df = df.sort_values(by=['date_int', 'geoId'])
df['days'].min()
df['dateRep'].max()
today = pd.to_datetime('today')
df = df.where(df['dateRep'] < today)
df = df.where(df['days'] >= 0)
df = df.dropna()
df.dtypes
import pandas as pd
import altair as alt
# select a point for which to provide details-on-demand
source = df.where(df['max_cases'] >= 10000)
label = alt.selection_single(
encodings=['x'], # limit selection to x-axis value
on='mouseover', # select on mouseover events
nearest=True, # select data point nearest the cursor
empty='none' # empty selection includes no data points
)
# define our base line chart of stock prices
base = alt.Chart().mark_line().encode(
alt.X('dateRep:T'),
alt.Y('cum_cases:Q', scale=alt.Scale(type='log')),
alt.Color('pais:N')
)
alt.layer(
base, # base line chart
# add a rule mark to serve as a guide line
alt.Chart().mark_rule(color='#aaa').encode(
x='dateRep:T'
).transform_filter(label),
# add circle marks for selected time points, hide unselected points
base.mark_circle().encode(
opacity=alt.condition(label, alt.value(1), alt.value(0))
).add_selection(label),
# add white stroked text to provide a legible background for labels
base.mark_text(align='left', dx=5, dy=-5, stroke='white', strokeWidth=5).encode(
text='cum_cases:Q'
).transform_filter(label),
base.mark_text(align='left', dx=35, dy=-5, stroke='white', strokeWidth=5).encode(
text='pais:N'
).transform_filter(label),
# add text labels for stock prices
base.mark_text(align='left', dx=5, dy=-5).encode(
text='cum_cases:Q'
).transform_filter(label),
base.mark_text(align='left', dx=35, dy=-5).encode(
text='pais:N'
).transform_filter(label),
data=source
).properties(
width=700,
height=400
)
# select a point for which to provide details-on-demand
source = df.where(df['max_cases'] >= 1000)
label = alt.selection_single(
encodings=['x'], # limit selection to x-axis value
on='mouseover', # select on mouseover events
nearest=True, # select data point nearest the cursor
empty='none' # empty selection includes no data points
)
# define our base line chart of stock prices
base = alt.Chart().mark_line().encode(
alt.X('days:Q'),
alt.Y('cum_cases:Q', scale=alt.Scale(type='log')),
alt.Color('pais:N')
)
alt.layer(
base, # base line chart
# add a rule mark to serve as a guide line
alt.Chart().mark_rule(color='#aaa').encode(
x='days:Q'
).transform_filter(label),
# add circle marks for selected time points, hide unselected points
base.mark_circle().encode(
opacity=alt.condition(label, alt.value(1), alt.value(0))
).add_selection(label),
# add white stroked text to provide a legible background for labels
base.mark_text(align='left', dx=5, dy=-5, stroke='white', strokeWidth=2).encode(
text='cum_cases:Q'
).transform_filter(label),
# add text labels for stock prices
base.mark_text(align='left', dx=5, dy=-5).encode(
text='cum_cases:Q'
).transform_filter(label),
base.mark_text(align='left', dx=40, dy=-5).encode(
text='pais:N'
).transform_filter(label),
data=source
).properties(
width=700,
height=400
)
source = df.where(df['cases'] > 0)
source = source.where(df['max_cases'] >= 30000)
alt.Chart(source).mark_line(point=True).encode(
alt.X('cum_cases:Q', scale=alt.Scale(type='log')),
alt.Y('cases:Q', scale=alt.Scale(type='log')),
color='pais',
tooltip=['pais:N', 'days:Q', 'cum_cases:Q']
).properties(
width=700,
height=400)
# select a point for which to provide details-on-demand
source = df.where(df['cases'] > 0)
source = source.where(df['max_cases'] >= 30000)
label = alt.selection_single(
encodings=['x'], # limit selection to x-axis value
on='mouseover', # select on mouseover events
nearest=True, # select data point nearest the cursor
empty='none' # empty selection includes no data points
)
# define our base line chart of stock prices
base = alt.Chart().mark_line(point=True).encode(
alt.X('cum_cases:Q', axis=alt.Axis(grid=False), scale=alt.Scale(type='log')),
alt.Y('cases:Q', axis=alt.Axis(grid=False), scale=alt.Scale(type='log')),
alt.Color('pais:N')
)
alt.layer(
base, # base line chart
# add a rule mark to serve as a guide line
alt.Chart().mark_rule(color='#aaa').encode(
x='cum_cases:Q'
).transform_filter(label),
# add circle marks for selected time points, hide unselected points
base.mark_circle().encode(
opacity=alt.condition(label, alt.value(1), alt.value(0))
).add_selection(label),
# add white stroked text to provide a legible background for labels
base.mark_text(align='left', dx=5, dy=-5, stroke='white', strokeWidth=2).encode(
text='cum_cases:Q'
).transform_filter(label),
# add text labels for stock prices
base.mark_text(align='left', dx=5, dy=-5).encode(
text='cum_cases:Q'
).transform_filter(label),
base.mark_text(align='left', dx=40, dy=-5).encode(
text='pais:N'
).transform_filter(label),
data=source
).properties(
width=700,
height=400
)
# select a point for which to provide details-on-demand
source = df#.where(df['max_cases'] >= 1000)
label = alt.selection_single(
encodings=['x'], # limit selection to x-axis value
on='mouseover', # select on mouseover events
nearest=True, # select data point nearest the cursor
empty='none' # empty selection includes no data points
)
# define our base line chart of stock prices
base = alt.Chart().mark_line().encode(
alt.X('dateRep:T'),
alt.Y('cum_deaths:Q'),
alt.Color('pais:N')
)
alt.layer(
base, # base line chart
# add a rule mark to serve as a guide line
alt.Chart().mark_rule(color='#aaa').encode(
x='dateRep:T'
).transform_filter(label),
# add circle marks for selected time points, hide unselected points
base.mark_circle().encode(
opacity=alt.condition(label, alt.value(1), alt.value(0))
).add_selection(label),
# add white stroked text to provide a legible background for labels
base.mark_text(align='right', dx=5, dy=-5, stroke='white', strokeWidth=5).encode(
text='cum_deaths:Q'
).transform_filter(label),
base.mark_text(align='right', dx=5, dy=-15, stroke='white', strokeWidth=5).encode(
text='pais:N'
).transform_filter(label),
# add text labels for stock prices
base.mark_text(align='right', dx=5, dy=-5).encode(
text='cum_deaths:Q'
).transform_filter(label),
base.mark_text(align='right', dx=5, dy=-15).encode(
text='pais:N'
).transform_filter(label),
data=source
).properties(
width=700,
height=400
).interactive()
# select a point for which to provide details-on-demand
source = df#.where(df['max_cases'] >= 1000)
label = alt.selection_single(
encodings=['x'], # limit selection to x-axis value
on='mouseover', # select on mouseover events
nearest=True, # select data point nearest the cursor
empty='none' # empty selection includes no data points
)
# define our base line chart of stock prices
base = alt.Chart().mark_line().encode(
alt.X('days:Q'),
alt.Y('cum_deaths:Q'),
alt.Color('pais:N')
)
alt.layer(
base, # base line chart
# add a rule mark to serve as a guide line
alt.Chart().mark_rule(color='#aaa').encode(
x='days:Q'
).transform_filter(label),
# add circle marks for selected time points, hide unselected points
base.mark_circle().encode(
opacity=alt.condition(label, alt.value(1), alt.value(0))
).add_selection(label),
# add white stroked text to provide a legible background for labels
base.mark_text(align='left', dx=5, dy=-5, stroke='white', strokeWidth=2).encode(
text='cum_deaths:Q'
).transform_filter(label),
base.mark_text(align='left', dx=37, dy=-5, stroke='white', strokeWidth=2).encode(
text='pais:N'
).transform_filter(label),
# add text labels for stock prices
base.mark_text(align='left', dx=5, dy=-5).encode(
text='cum_deaths:Q'
).transform_filter(label),
base.mark_text(align='left', dx=37, dy=-5).encode(
text='pais:N'
).transform_filter(label),
data=source
).properties(
width=700,
height=400
)
date = data['dateRep'].max()
source = df.where(df['dateRep'] >= date)
source = source.where(source['max_cases'] >= 1000)
alt.Chart(source).mark_circle(size=100).encode(
alt.X('cum_cases:Q'),
alt.Y('cum_deaths:Q', scale=alt.Scale(type='log')),
color='pais',
tooltip=['pais:N', 'cum_deaths:Q', 'cum_cases:Q']
).properties(
width=700,
height=400
).interactive()
source = source.where(source['max_cases'] >= 1000)
alt.Chart(source).mark_circle(size=100).encode(
alt.X('cum_cases:Q'),
alt.Y('cum_deaths:Q'),
color='pais',
tooltip=['pais:N', 'cum_deaths:Q', 'cum_cases:Q']
).properties(
width=700,
height=400
).interactive()
# select a point for which to provide details-on-demand
source = df.where(df['pais'] == 'Argentina')
label = alt.selection_single(
encodings=['x'], # limit selection to x-axis value
on='mouseover', # select on mouseover events
nearest=True, # select data point nearest the cursor
empty='none' # empty selection includes no data points
)
# define our base line chart of stock prices
base = alt.Chart().mark_line().encode(
alt.X('days:Q'),
alt.Y('cum_cases:Q', scale=alt.Scale(type='log')),
alt.Color('pais:N')
)
alt.layer(
base, # base line chart
# add a rule mark to serve as a guide line
alt.Chart().mark_rule(color='#aaa').encode(
x='days:Q'
).transform_filter(label),
# add circle marks for selected time points, hide unselected points
base.mark_circle().encode(
opacity=alt.condition(label, alt.value(1), alt.value(0))
).add_selection(label),
# add white stroked text to provide a legible background for labels
base.mark_text(align='left', dx=5, dy=-5, stroke='white', strokeWidth=2).encode(
text='cum_cases:Q'
).transform_filter(label),
# add text labels for stock prices
base.mark_text(align='left', dx=5, dy=-5).encode(
text='cum_cases:Q'
).transform_filter(label),
data=source
).properties(
width=700,
height=400
)
# select a point for which to provide details-on-demand
source = df.where(df['pais'] == 'Argentina')
label = alt.selection_single(
encodings=['x'], # limit selection to x-axis value
on='mouseover', # select on mouseover events
nearest=True, # select data point nearest the cursor
empty='none' # empty selection includes no data points
)
# define our base line chart of stock prices
base = alt.Chart().mark_line().encode(
alt.X('days:Q'),
alt.Y('cum_deaths:Q'),
alt.Color('pais:N')
)
alt.layer(
base, # base line chart
# add a rule mark to serve as a guide line
alt.Chart().mark_rule(color='#aaa').encode(
x='days:Q'
).transform_filter(label),
# add circle marks for selected time points, hide unselected points
base.mark_circle().encode(
opacity=alt.condition(label, alt.value(1), alt.value(0))
).add_selection(label),
# add white stroked text to provide a legible background for labels
base.mark_text(align='left', dx=5, dy=-5, stroke='white', strokeWidth=2).encode(
text='cum_deaths:Q'
).transform_filter(label),
# add text labels for stock prices
base.mark_text(align='left', dx=5, dy=-5).encode(
text='cum_deaths:Q'
).transform_filter(label),
data=source
).properties(
width=700,
height=400
)
source = df.where(df['pais'] == 'Argentina')
alt.Chart(source).mark_line(point=True).encode(
alt.X('dateRep:T'),
alt.Y('cases:Q', scale=alt.Scale(type='log')),
color='pais',
tooltip=['pais:N', 'dateRep:T', 'cum_cases:Q']
).properties(
width=700,
height=400)